package app.ui.panels {
	import app.model.Time;
	import flash.events.Event;

	import com.bit101.components.ComboBox;
	import bme.data.BMELoader;

	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import com.bit101.components.TextArea;

	import flash.display.DisplayObjectContainer;
	import flash.text.TextFieldAutoSize;

	/**
	 * @author Rendall
	 */
	public class BMELoadPanel extends AbstractBrowserPanel {

		//private static const ASSET_URL : String = "http://earth.burningman.com/api/0.1/2010/event/";		private static const ASSET_URL : String = "assets/event2010";
		private static const BASE_URL:String = "http://earth.burningman.com/api";
		private static const API_VERSION:String = "0.1";
		private static const SERVICE:String = "event";
		
		public static const STATUS_NEW_ITEM : String = "STATUS_NEW_ITEM";
		public static const STATE_COMPLETE : String = "STATE_COMPLETE";

		private var bmeLoader : BMELoader;
		private var statusText : TextArea;
		private var bmeUrlInput : InputText;
		private var sendButton : PushButton;
		private var cancelButton : PushButton;
		private var yearCombo : ComboBox;
		private var yearLabel : Label;
		private var service : String;
		private var apiv : String;
		private var baseurl : String;
		private var yearsArr : Array;

		private function onURLChange(event : Event) : void {
			var url : String = baseurl + "/" + apiv + "/" + year + "/" + service + "/";
			bmeUrlInput.text = url;
		}
		
		public function BMELoadPanel(parent : DisplayObjectContainer = null, xpos : Number = 0, ypos : Number = 0) {
			super(parent, xpos, ypos);

			yearsArr = new Array();
			
			var currYear : Number = new Date().getFullYear();
			for (var i:int = currYear; i >= 2009; i--) yearsArr.push(i.toString());
			//trace ("\tyearsArr : " + yearsArr);

			setUI();
				
			bmeLoader = BMELoader.getInstance();
			bmeLoader.statusSignal.add(onLoadStatusChange);
			
			baseurl = BASE_URL;
			apiv = API_VERSION;
			service = SERVICE;
			
			

			var isStandAlone : Boolean = this.parent != null;
			if (isStandAlone) standAloneInit();
		}

		private function setUI() : void {
			
			setTitle("Update Data");
			
			statusText = new TextArea(this,5,30);
			statusText.setSize(540, 20);
			statusText.html = true;
			
			
			
			var bmeLabel : Label = new Label(this, 5, 65, "Event URL: ");
			var bmeUrlX : Number = bmeLabel.x + bmeLabel.width + 5;
			bmeUrlInput = new InputText(this, bmeUrlX, 65);
			bmeUrlInput.textField.autoSize = TextFieldAutoSize.LEFT;
			bmeUrlInput.text = ASSET_URL;
			bmeUrlInput.width = 550 - bmeUrlInput.x - 5;
			
			
			yearLabel = new Label(this, 5, 95, "Year: ");
			yearLabel.autoSize = true;
			yearCombo = new ComboBox(this, yearLabel.width + 5, 95, null, yearsArr);
			yearCombo.selectedIndex = new Date().month <= Time.JUNE? 1:0; // show previous year by default before June.
			yearCombo.addEventListener(Event.SELECT, onURLChange);
			


			sendButton = new PushButton(this, 5, 400-30, "UPDATE", onSendPress);
			sendButton.tabEnabled = true;
			
			cancelButton = new PushButton(this, sendButton.width + 10, 400-30, "CANCEL", onCancelPress);
			cancelButton.tabEnabled = true;
			
			enableSend(true);
		}

		private function onCancelPress(event : *) : void {
			bmeLoader.cancelRequest();
			enableSend(true);
		}

		
		private function onSendPress(input : *) : void {
			trace("\n BMELoadPanel.onSendPress( input:" + input + ")");
			onInput();
			enableSend(false);
		}

		private function onInput() : void {
			var url : String = bmeUrlInput.text;
			bmeLoader.sendRequest(url);
		}

		
		private function onLoadStatusChange(status : String, message : *) : void {

			switch (status) {
				case BMELoader.STATUS_READY: 
					message = "Load data from Burning Man Earth.";
					if (stage) stage.focus = bmeUrlInput.textField;
					enableSend(true);
					break;                    
				case BMELoader.STATUS_REQUEST_SENT:
					message = "Please wait."; 
					enableSend(false);
					break;
				case BMELoader.STATUS_ERROR:
					message = "An error has occurred. " + message;
					enableSend(true);
					break;
				case BMELoader.STATUS_PARSE_COMPLETE:
					stateChangeSignal.dispatch(STATE_COMPLETE, "all done");
					break;
			}
			
			
			var statusMessage : String = status;
			if (message != null) statusMessage += " : " + message;
			
			var firstText : Boolean = BMELoadPanel.isEmptyString(statusText.text);
			if (!firstText) statusText.text = statusMessage + "\n" + statusText.text;
			else statusText.text = statusMessage;
		
			trace("\n BMELoadPanel.onLoadStatusChange( status:" + status + ")");
		}

		// For debugging purposes, if this is testing the BMELoadPanel as a lone swf.
		private function standAloneInit() : void {
			trace("\n BMELoadPanel.standAloneInit( )");
			this.width = 550;
			this.height = 400;
		}

		private static function isEmptyString(str : String) : Boolean {
			return (str == null || str == "" || str.length == 0);	
		}

		private function enableSend(doEnable:Boolean):void{
			sendButton.enabled = doEnable;
			cancelButton.enabled = !doEnable;	
			
		}
		
		private function get year():String{
			return yearCombo.selectedItem as String;	
		}
	}
}
